// TODO megaWhere alias where!
where = function(){
	var a=arguments;
	if(Ext.isArray(a[0])){
		parts = Array.prototype.slice.call(a);
		return multiWhere(parts);
	} else {
		return simpleWhere(a[0],a[1],a[2]);
	}
}


simpleWhere = function (name,val,sign){
	return Ext.encode({
		parts:[{
			field:name,
			value:val,
			sign:sign || '='
		}]
	});
}

// moreWhere([['fld','val',<'equity'>]['fld','val',<'eq','OR'>]]);
moreWhere = function(parts){
	/*
	if(arguments.length > 1){
		parts = Array.prototype.slice.call(arguments);
	}*/
	
	var prod = [];
	for(var i=0;i<parts.length;i++){
		var x = parts[i];
		if (typeof x !== 'undefined'){
			if(x.length){// !x.parts
				var y = {field:x[0],value:x[1],sign:x[2] || '='};
				//if(x[3])y.operator = x[3];
				//else if(prod.length>0)y.operator = 'AND'
				if(prod.length>0)y.operator = x[3] || 'AND';
				
				prod.push(y);
			} else {
				var sprod = [];
				for(var j=0;j<x.parts.length;j++){
					var sx = x.parts[j];
					var sy = {field:sx[0],value:sx[1],sign:sx[2]||'='}
					//if(sx[3])sy.operator = sx[3];
					//else if(sprod.length>0)sy.operator = 'AND'
					if(sprod.length>0)sy.operator = sx[3] || 'AND';
					sprod.push(sy);
				}
				var tprod = {parts:sprod};
				//if(x.operator)tprod.operator = x.operator;
				//else if(prod.length>0)tprod.operator = 'AND'
				if(prod.length>0)tprod.operator = x.operator || 'AND'; 
				prod.push(tprod);
			}
		}
	}
	return Ext.encode({parts:prod});
}

multiWhere = function(parts){
	var prod = [];
	for(i=0;i<parts.length;i++){
		var x = parts[i];
		if (typeof x !== 'undefined'){
			if(x.parts){
				var temp = []
				for(j=0;j<x.parts.length;j++){
					var y = {field:x.parts[j][0],value:x.parts[j][1],sign:x.parts[j][2] || '='};
					//if(x.parts[j][3])y.operator = x.parts[j][3];
					//else if(temp.length>0)y.operator = 'AND'
					if(temp.length>0)y.operator = x.parts[j][3] || 'AND';
					temp.push(y);
				}
				prod.push({parts:temp,operator:prod.length>0 ? (x.operator || 'AND') : null});
			} else {
				var y = {field:x[0],value:x[1],sign:x[2]||'='};
				//if(x[3])y.operator = x[3];
				//else if(prod.length>0)y.operator = 'AND'
				if(prod.length>0)y.operator = x[3] || 'AND';
				prod.push(y);
			}
		}
	}
	
	return prod.length>0 ? Ext.encode({parts:prod}) : null;
} 